package leetcode.easy;

import leetcode.base.TreeNode;

/**
 * ClassName: $110_IsBalanced
 * Description:
 * date: 2021-11-18 22:15
 *
 * @author liyifan
 */
public class $110_IsBalanced {
    public boolean isBalanced(TreeNode root) {
        return dfs(root) != -1;
    }

    public int dfs(TreeNode root) {
        if (root == null)
            return 0;
        int left = dfs(root.left);
        if (left == -1)
            return -1;
        int right = dfs(root.right);
        if (right == -1)
            return -1;
        return Math.abs(left - right) > 1 ? -1: Math.max(left, right) + 1;
    }
}
